﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

using DM_Article.Bll;
using DM_Article.Model;

public partial class admin_admin_article : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script language='javascript'>alert('非法操作，请先登录！');location.href = 'admin_login.aspx';</script>");
            }
            else
            {
                getarticle();
            } 
        }
    }

    private void getarticle()
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
        {
            this.GV_article.DataSource = articlebll.getArticleTable(cn).DefaultView;
            this.GV_article.DataBind();
        }
    }

    protected void GV_article_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            this.GV_article.PageIndex = e.NewPageIndex;
            getarticle();
        }
        catch
        {
 
        }
    }

    protected void DlPage_Init(object sender, EventArgs e)//DlPage就是分页中的下拉列表框
    {
        DropDownList list = (DropDownList)sender;
        for (int i = 1, j = GV_article.PageCount; i <= j; i++)
        {
            list.Items.Add(new ListItem(i.ToString()));
        }
        list.SelectedIndex = GV_article.PageIndex;
    }

    protected void DlPage_SelectedIndexChanged(object sender, EventArgs e)//DlPage就是分页中的下拉列表框
    {
        DropDownList list = (DropDownList)sender;
        GV_article.PageIndex = list.SelectedIndex;
        getarticle();
    }


    protected void GV_article_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = this.GV_article.Rows[e.NewEditIndex].Cells[0].Text;
        Response.Redirect("admin_articleedit.aspx?id=" + id);
    }


    protected void GV_article_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
        {
            cn.Open();
            string id = this.GV_article.Rows[e.RowIndex].Cells[0].Text;
            if (articlebll.delete(cn, id) > 0)
            {
                Response.Write("<script language='javascript'>alert('删除成功！');location.href = 'admin_article.aspx';</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('删除失败！');location.href = 'admin_article.aspx';</script>");
            }
        }
    }
    protected void GV_article_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
            {
                e.Row.Cells[2].Text = bigclassbll.getBigclassDetail(cn, e.Row.Cells[2].Text).bigclassname;
                e.Row.Cells[3].Text = smallclassbll.getSmallclassDetail(cn, e.Row.Cells[3].Text).smallclassname;
            }
        }
    }
}
